package com.onpositive.notes.android.main;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import com.onpositive.notes.android.ui.elements.MenuUI;
import com.onpositive.notes.model.INote;
import com.onpositive.notes.services.BasicCollectionRegistry;
import com.onpositive.notes.services.BasicNoteRegistry;

public class MainActivity extends Activity {

	boolean deleteData = false ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		

		if( BasicNoteRegistry.getInstance().getAllNotes().isEmpty() )
			load() ;

		MenuUI menu = new MenuUI() ;
		View view = menu.createView(this) ; 
		setContentView(view);
	}
	
	protected void load(){
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {			
			System.out.println( "can write" );			
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			System.out.println( "can read only" );
		} else {
			System.out.println( "can do nothing" );
			return ;
		}
		
		while( true ){
			File dir = Environment.getExternalStoragePublicDirectory("notes-data");
			if(!dir.exists())
				break ;
			
			File notePath = new File( dir, "notes.dat") ;
			File collectionsPath = new File( dir, "collections.dat") ;
			
			BasicNoteRegistry.loadInstance( notePath.getPath() ) ;
			BasicCollectionRegistry.loadInstance( collectionsPath.getPath() ) ;
			
			break ;
		}
		//BasicNoteRegistry.getInstance().getAllNotes().clear() ;
		//BasicCollectionRegistry.getInstance().clear() ;
		if(! BasicNoteRegistry.getInstance().getAllNotes().isEmpty() )
			return ;

		CommonResources.initNotes() ;
		for( Object obj: CommonResources.noteList ){
			
			INote note = (INote) obj ;
			BasicNoteRegistry.getInstance().registerNote(note) ;
		}
	}
	
	protected void save(){
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {			
			System.out.println( "can write" );
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			System.out.println( "can read only" );
			return ;
		} else {
			System.out.println( "can do nothing" );
			return ;
		}

		File dir = Environment.getExternalStoragePublicDirectory("notes-data");
		dir.mkdirs() ;
		
		File notePath = new File( dir, "notes.dat") ;
		File collectionsPath = new File( dir, "collections.dat") ;
			
		BasicNoteRegistry.saveInstance( notePath.getPath() ) ;
		BasicCollectionRegistry.saveInstance( collectionsPath.getPath() ) ;
	}
	
	@Override
	public void finish() {
		if(deleteData)
			deleteData() ;
		else
			save() ;
		
		super.finish();
	}
	
	protected void deleteData(){
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {			
			System.out.println( "can write" );			
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			System.out.println( "can read only" );
		} else {
			System.out.println( "can do nothing" );
			return ;
		}
		BasicNoteRegistry.disposeInstance() ;
		BasicCollectionRegistry.disposeInstance() ;
		
		
		while( true ){
			File dir = Environment.getExternalStoragePublicDirectory("notes-data");
			if(!dir.exists())
				break ;
			
			delete( dir ) ;
			break ;
		}
	}

	private void delete(File file) {
		if (file.isDirectory()) {
			File[] listFiles = file.listFiles();
			for (File f : listFiles)
				delete(f);
		}
		file.delete();
	}

	public boolean isDeleteData() {
		return deleteData;
	}

	public void setDeleteData(boolean deleteData) {
		this.deleteData = deleteData;
	}
}
